﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using QuTouTiaoAssist.Util;

namespace QuTouTiaoAssist
{
    public partial class FrmFindPwd : FrmOkCancelBase
    {
        public FrmFindPwd()
        {
            InitializeComponent();
            ValidateRules.Add(this.textBox1, new ControlTextValidate(this.textBox1, OtherUtil.IsMobile, "请输入11位有效的手机号！", true));
            ValidateRules.Add(this.textBox2, new ControlTextValidate(this.textBox2, OtherUtil.IsNullOrWhitespace, "短信验证码不能为空！", true, true));
            ValidateRules.Add(this.textBox3, new ControlTextValidate(this.textBox3, string.IsNullOrEmpty, "密码不能为空！", false, true));
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            this.textBox3.PasswordChar = this.checkBox1.Checked ? '\0' : '*';
        }

        private void btnGetVerifyCode_Click(object sender, EventArgs e)
        {
            string mobile = this.textBox1.Text.Trim();
            if (!CheckValidateRule(this.textBox1))
                return;

            string imgCaptchaId = null;
            var frm = new FrmImageVerifyCode();
            frm.RefreshImage += (o, args) =>
            {
                try
                {
                    var captchaResult = QttHelper.GetImgCaptcha2();
                    if (captchaResult.Code != 0)
                    {
                        throw new Exception("获取验证码失败，" + captchaResult.Message);
                    }
                    args.Img = captchaResult.Data.AnalyzeImageData();
                    imgCaptchaId = captchaResult.Data.ImageId;
                }
                catch (Exception ex)
                {
                    args.Img = null;
                }
            };
            if (frm.ShowDialog() == DialogResult.OK)
            {
                new Thread(() =>
                {
                    try
                    {
                        var smsResult = QttHelper.GetSmsCaptcha2(mobile, frm.VerifyCode, imgCaptchaId);
                        if (smsResult.Code == 0)
                        {
                            MsgBox.ShowInfo("短信验证码已经发送您的手机！");
                            return;
                        }
                        else
                        {
                            throw new Exception(smsResult.Message);
                        }
                    }
                    catch (Exception ex)
                    {
                        MsgBox.ShowInfo("获取短信验证码失败，" + ex.Message);
                    }
                }){IsBackground = true}.Start();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string mobile = this.textBox1.Text.Trim();
            string smsCaptcha = this.textBox2.Text.Trim();
            string pwd = this.textBox3.Text;
            if(!CheckValidateRule(this.textBox1)||
                !CheckValidateRule(this.textBox2)||
                !CheckValidateRule(this.textBox3))
            {
                return;
            }

            new Thread(() =>
            {
                try
                {
                    var checkResult = QttHelper.CheckSmsCaptcha(smsCaptcha, mobile, "2");
                    if (checkResult.Code == 0)
                    {
                        var findResult = QttHelper.FindPassword(smsCaptcha, mobile, pwd);
                        if (findResult.Code == 0)
                        {
                            MsgBox.ShowInfo("密码重置成功，新密码为：" + pwd + "!");
                            base.ReturnOk();
                            return;
                        }
                        else
                        {
                            throw new Exception("找回密码失败，" + checkResult.Message);
                        }
                    }
                    else
                    {
                        throw new Exception("检查短信验证码失败，" + checkResult.Message);
                    }
                }
                catch (Exception ex)
                {
                    MsgBox.ShowInfo(ex.Message);
                }
            }){IsBackground = true}.Start();
        }
    }
}
